<?php
/**
 * Created by PhpStorm.
 * User: 24369
 * Date: 2021/6/25
 * Time: 20:58
 */
namespace app\admin\controller;

use app\admin\BaseController;
use app\admin\logic\BrandLogic;
use app\admin\logic\GoodLogic;
use think\facade\Request;
use think\facade\View;

class Brand extends BaseController
{
    public function brandList()
    {
        //搜索条件
        $where=BrandLogic::getwhere();
        //搜索关键字
        $keywords=Request::param('keywords');
        //品牌查询
        $list=\app\admin\model\Brand::getBrandList($where,$keywords);
        View::assign('list',$list);
        return view();
    }
    //品牌添加
    public function brandAdd()
    {
        if (Request::isPost()){
            //验证品牌添加数据
            list($msg,$post)=BrandLogic::getBrandData();
            if (isset($msg) && $msg != '')      $this->error($msg);
            //图片上传
            list($msg,$path)=GoodLogic::getImg('img','UploadFiles/data/files');
            if (isset($msg) && $msg != '')      $this->error($msg);
            if (isset($path) && $path != '')    $post['brand_logo']=$path;
            //选框不选时默认为0
            $post['recommended']=isset($post['recommended'])?$post['recommended']:0;
            $post['if_show']=isset($post['if_show'])?$post['if_show']:0;
            //品牌添加
            $res=\app\admin\model\Brand::create($post);
            if ($res){
                $this->success('添加成功',url('Brand/brandAdd'));
            }else{
                $this->error('添加失败');
            }
        }else{
            return  view();
        }
    }
    //品牌修改
    public function brandEdit()
    {
        $id=Request::param('id');
        if (Request::isPost()){
            //修改品牌数据验证
            list($msg,$post)=BrandLogic::getBrandData();
            if (isset($msg) && $msg != '')      $this->error($msg);
            //修改品牌图片上传
            list($msg,$path)=GoodLogic::getImg('img','UploadFiles/data/files');
            if (isset($msg) && $msg != '')      $this->error($msg);
            if (isset($path) && $path != '')    $post['brand_logo']=$path;
            //选框不选默认为0

            $post['recommended']=isset($post['recommended'])?$post['recommended']:0;
            $post['if_show']=isset($post['if_show'])?$post['if_show']:0;
            //修改数据id
            $post['brand_id']=$id;
            //品牌修改
            $res=\app\admin\model\Brand::update($post);
            if ($res){
                $this->success('修改成功',url('Brand/brandList'));
            }else{
                $this->error('修改失败');
            }
        }else{
            if ($id){
                $brand_edit=\app\admin\model\Brand::field(true)->find($id)->toArray();
                View::assign($brand_edit);
            }
            return view();
        }
    }
    //品牌软删除
    public function brandSoftDelete()
    {
        $id=Request::param('id');
        if ($id){
            //品牌删除
            $res=\app\admin\model\Brand::destroy($id);
            if ($res){
                $this->success('删除成功',url('Brand/brandList'));
            }else{
                $this->error('删除失败',url('Brand/brandList'));
            }
        }
    }
    //品牌回收
    public function brandRecover()
    {
        //搜索条件
        $where=BrandLogic::getwhere();
        //关键字
        $keywords=Request::param('keywords','');
        //品牌回收展示
        $list=\app\admin\model\Brand::getBrandRecover($where,$keywords);
        View::assign('list',$list);
        return  view();
    }
    //品牌恢复
    public function brandResume()
    {
        $id=Request::param('id');
        if ($id){
            $brand=\app\admin\model\Brand::onlyTrashed()->find($id);
            $brand->restore();
            if ($brand){
                $this->success('恢复成功',url('Brand/brandRecover'));
            }else{
                $this->error('恢复失败',url('Brand/brandRecover'));
            }
        }
    }
    //品牌删除
    public  function brandDelete()
    {
        $id=Request::param('id');
        if ($id){
            $brand=\app\admin\model\Brand::destroy($id,true);
            if ($brand){
                $this->success('删除成功',url('Brand/brandRecover'));
            }else{
                $this->error('删除失败',url('Brand/brandRecover'));
            }
        }
    }
}